global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/17_workers_separation_cross_year", replace


*Define incumbents based on Apr 2016 characteristics 
cap program drop clean
program define clean 
args ref i
	use client_id state yr_month emp_pur_c base0 salaried0 base if base0>=700 & base0<913 & salaried0==1 using "$output\unbalanced_panel_`ref'.dta", clear
	duplicates drop yr_month client_id emp_pur_c, force 
	
	*Define employed
		gen employed = 1
	
	*Expand to fill in time series
		gegen group=group(client_id emp_pur_c)
		if `i'==0 {
			gen time=mofd(yr_month)-mofd(mdy(4,01,2014))
		}
		if `i'==1 {
			gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
		}
		tsset group time 
		tsfill, full
				
		bysort group: carryforward client_id, replace
		bysort group: carryforward emp_pur_c, replace
		bysort group: carryforward base0, replace
			
		gsort group -time 
		bysort group: carryforward client_id, replace
		bysort group: carryforward emp_pur_c, replace
		bysort group: carryforward base0, replace
		
		drop group

	*Define unemployed
		replace employed=0 if employed==.
		gen treat=`i'
		
	*Save 
		if `i'==0 {
			save "$temp/separations_temp.dta", replace
		}
end 
clean control 0	
clean treat 1

append using "$temp/separations_temp.dta"

	
******************************************************
**Plot raw graphs
******************************************************
*Program for connected plot by base pay
preserve 
	collapse (mean) employed, by(treat time)

		label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
	label values time time_lab 
	
	graph twoway (connect employed time if treat==1, lc(black) mcolor(black)) ///
		(connect  employed time if treat==0, lc(maroon) mcolor(maroon) msymbol(D)), ///
		xline(1 8, lcolor(black)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
		ytitle("Share of Workers Employed") legend(order(1 "2016" 2 "2014") subtitle("Sample") ring(0) position(2) col(2) region(lstyle(solid) lcolor(black) lwidth(thin)))
	graph export "$graphs/employed_raw_year.eps", replace
	graph export "$graphs/employed_raw_year.jpg", replace
restore 


******************************************************
**Plot diff-in-diff
******************************************************
*Program to estimate regressions
cap program drop reg_est
program define reg_est 
args var name fe

	preserve 
	
	if `fe'== 0 {
	reghdfe `var' d_*, absorb(worker time) vce(cluster client_id)
	}
	if `fe'== 1 {
	reghdfe `var' d_*, absorb(worker client_id##time) vce(cluster client_id)
	}
	cap drop beta ub lb
	gen beta=.
	gen ub=.
	gen lb=.
	gen event_time=.
		
	local index=1
	forvalues i=-11/25 {
		replace event_time=`i' if _n==`index'
		if `i'!=0 {
			replace beta=_b[d_`index'] if _n==`index'
			replace ub=_b[d_`index'] + 1.96*_se[d_`index'] if _n==`index'
			replace lb=_b[d_`index'] - 1.96*_se[d_`index'] if _n==`index'	
		}
		if `i'==0 {
			replace beta=0 if _n==`index'
			replace ub=0 if _n==`index'
			replace lb=0 if _n==`index'
		}
		local ++index
	}
	
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
	save "$output/`name'_estimates`fe'_year.dta", replace
	restore
end 
	
	*Estimate diff-in-diff
	local index=1
	forvalues i=-11/25 {
		gen d_`index'= treat==1 & time==`i'
		label var d_`index' "Treat=1 and Event Time=`i'"
		local ++index
	}
	drop d_12
	egen worker=group(emp_pur_c client_id treat)
	
	reg_est employed employed_diff_in_diff 0
	reg_est employed employed_diff_in_diff 1
	

	
	
************************************************
*Program to estimate regressions for table
************************************************
cap program drop reg_est_tab
program define reg_est_tab
args var name

	preserve 
	
	gen p1=time<=0 & treat==1
	gen p2=time>0 & time<=8 & treat==1
	gen p3=time>8 & treat==1
	gen slope1=time*p1
	gen slope2=time*p2 
	gen slope3=time*p3 
	
	*Variables to save estimates
		gen spec=""
		gen controls=""
		forvalues i=1/3 {
			gen beta`i'=.
			gen se`i'=.
		}
		gen n=.
		gen beta_change=.
		gen se_change=.
		
	*Regression
	reghdfe `var' p2 p3 slope1 slope2 slope3, nocons absorb(worker time) vce(cluster client_id)

	replace spec="`spec'" if _n==1
	replace controls="Worker Time" if _n==1
	replace beta1=_b[slope1] if _n==1
	replace se1=_se[slope1] if _n==1
	replace beta2=_b[slope2] if _n==1
	replace se2=_se[slope2] if _n==1
	replace beta3=_b[slope3] if _n==1
	replace se3=_se[slope3] if _n==1
	replace n=`e(N)' if _n==1
	
	nlcom _b[slope3]-_b[slope2]
	replace beta_change=r(b)[1,1] if _n==1
	replace se_change=sqrt(r(V)[1,1]) if _n==1

	*Regression within firm
	reghdfe `var' p2 p3 slope1 slope2 slope3, nocons absorb(worker client_id##time) vce(cluster client_id)

	replace spec="`spec'" if _n==2
	replace controls="Worker Firm-Time" if _n==2
	replace beta1=_b[slope1] if _n==2
	replace se1=_se[slope1] if _n==2
	replace beta2=_b[slope2] if _n==2
	replace se2=_se[slope2] if _n==2
	replace beta3=_b[slope3] if _n==2
	replace se3=_se[slope3] if _n==2
	replace n=`e(N)' if _n==2
	
	nlcom _b[slope3]-_b[slope2]
	replace beta_change=r(b)[1,1] if _n==2
	replace se_change=sqrt(r(V)[1,1]) if _n==2
		
	sum `var' if treat==1 & time==7
	replace beta_change=`r(mean)' if _n==3
	
	keep if _n<=3
	keep spec controls beta* se* n
	
	*Save estimates
	save "$output/`name'_table_estimates_year.dta", replace
	
	restore 
end 	

reg_est_tab employed employed

	
log close
